#! /bin/bash

script_dir=$(dirname $(realpath $0))
icon_path=$script_dir/icons
output_path=$script_dir/assets/img
temp_path=/tmp/$(basename $0).dir

# 检查图标路径
if [ ! -d $icon_path ]; then
    echo "icons path $icon_path not exisits!!"
fi

# 创建临时目录
if [ ! -d $temp_path ]; then
    mkdir -p $temp_path
fi

# 创建输出目录
if [ ! -d $output_path ]; then
    mkdir -p $output_path
fi

# 默认参数
default_size=128

if [ $# -eq 1 ]; then
    default_size=$1
fi

# 配置参数
width=$default_size
height=$default_size
background_color="#ffffff"
background_opacity=0

function proccess_svg()
{
    if [ $# -ne 1 ]; then
        return
    fi
    
    file_name=$1
    origin_file_path=$icon_path/$file_name
    input_file_path=$temp_path/$file_name
    out_file_name=${file_name/.svg/.png}
    out_file_path=$output_path/$out_file_name
    # copy file to temp
    cp -f $origin_file_path $input_file_path
    # change fill color
    sed -i -e 's/currentColor/#EEEEEE/g' -e 's/#000/#D29C45/g' $input_file_path
    # convert svg to png
    inkscape -w $width -h $height -b $background_color -y $background_opacity -f $input_file_path -e $out_file_path > /dev/null
    # remove temperary file
    rm -f $input_file_path
}

N=$(nproc)
i=1
for svg in $(ls $icon_path --hide="*-fill.svg"); do
    ((i=i%N)); ((i++==0)) && wait
    echo "proccessing file $svg"
    proccess_svg $svg &
done

wait

echo "Done!"

